﻿using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Util.NoSQLHelpers;

namespace AllSqlHelperWin.NoSQLHelperForms.MongoDBForms
{
    public partial class MongoDBUpdateForm : Form
    {
        /// <summary>
        /// 用户信息
        /// </summary>
        UserModel_MongoDB _userModel;

        public MongoDBUpdateForm()
        {
            InitializeComponent();
        }

        public MongoDBUpdateForm(UserModel_MongoDB userModel)
        {
            _userModel = userModel;
            InitializeComponent();
            cmbClass.SelectedIndex = 0;
        }

        private void MysqlUpdateForm_Load(object sender, EventArgs e)
        {
            txtName.Text = _userModel.Name;
            cmbClass.Text = _userModel.Class;
            txtAge.Text = _userModel.Age.ToString();
            if (_userModel.Gender)
            {
                rdBtnB.Checked = true;
            }
            else
            {
                rdBtnG.Checked = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("名称不可为空！");
                return;
            }

            string classStr = cmbClass.Text.Trim();
            string ageStr = txtAge.Text.Trim();
            bool gender = rdBtnB.Checked;

            _userModel.Name = name;
            _userModel.Class = classStr;
            _userModel.Age = string.IsNullOrEmpty(ageStr) ? 0 : Convert.ToInt32(ageStr);
            _userModel.Gender = gender;

            var list = new List<UpdateDefinition<UserModel_MongoDB>>();
            foreach (var item in _userModel.GetType().GetProperties())  // 转化为key-value的形式
            {
                list.Add(Builders<UserModel_MongoDB>.Update.Set(item.Name, item.GetValue(_userModel)));
            }

            var collection = MongoDBHelper.GetCollection<UserModel_MongoDB>("User");
            UpdateResult updeResult = MongoDBHelper.UpdateOne(collection, Builders<UserModel_MongoDB>.Filter.Eq("_id", _userModel._id),
                Builders<UserModel_MongoDB>.Update.Combine(list));
            if (updeResult.IsAcknowledged)
            {
                MessageBox.Show("更新成功！");
            }
            else
            {
                MessageBox.Show("更新失败！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("名称不可为空！");
                return;
            }

            string classStr = cmbClass.Text.Trim();
            string ageStr = txtAge.Text.Trim();
            bool gender = rdBtnB.Checked;

            _userModel.Name = name;
            _userModel.Class = classStr;
            _userModel.Age = string.IsNullOrEmpty(ageStr) ? 0 : Convert.ToInt32(ageStr);
            _userModel.Gender = gender;

            var list = new List<UpdateDefinition<UserModel_MongoDB>>();
            foreach (var item in _userModel.GetType().GetProperties())  // 转化为key-value的形式
            {
                list.Add(Builders<UserModel_MongoDB>.Update.Set(item.Name, item.GetValue(_userModel)));
            }

            var collection = MongoDBHelper.GetCollection<UserModel_MongoDB>("User");
            UpdateResult updeResult = MongoDBHelper.UpdateMany(collection, Builders<UserModel_MongoDB>.Filter.Eq("_id", _userModel._id),  // UpdateMany
                Builders<UserModel_MongoDB>.Update.Combine(list));
            if (updeResult.IsAcknowledged)
            {
                MessageBox.Show($"更新成功！");
            }
            else
            {
                MessageBox.Show("更新失败！");
            }
        }

        /// <summary>
        /// 只能输入数字
        /// </summary>
        private void txt1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果输入的不是退格和数字，则屏蔽输入
            if (!(e.KeyChar == 8 || (e.KeyChar >= 48 && e.KeyChar <= 57)))  // 小数点为46
            {
                e.Handled = true;
            }
        }
    }
}
